package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.dao.impl.activity.pixiu.PiXiuUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.pixiu.PiXiuUserData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.pixiu.PiXiuActivityMgr;
import com.yanqu.road.server.manger.activity.pixiu.PiXiuConfig;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class PiXiuRecycleStrategy extends AbstractItemRecycleStrategy  {

    private static PiXiuConfig historyConfig = null;

    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = PiXiuActivityMgr.getActivityInfo();

        if (ActivityMgr.activityInShowTime(activityInfo)) {
            long recycleTime = activityInfo.getEndShowTime() * 1000L + gapTime;
            PiXiuConfig config = PiXiuActivityMgr.getConfig();
            if (config != null) {
                List<String> paramList = new ArrayList<>();
                paramList.add(String.valueOf(config.getItemId()));
                paramList.add("");
                paramList.add("");
                paramList.add(config.getActivityInfo().getActivityName());
                ActivityItemRecycleMgr.addRecycleItemData(config.getItemId(), activityType, 1, recycleTime, paramList);

                //补发返利
                paramList = new ArrayList<>();
                //活动id
                paramList.add(String.valueOf(activityInfo.getActivityId()));
                paramList.add(config.getActivityInfo().getActivityName());
                ActivityItemRecycleMgr.addRecycleItemData(0, activityType, 5, recycleTime, paramList);

                if(historyConfig == null || historyConfig.getActivityId() != config.getActivityId()){
                    historyConfig = config;
                }
            }
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.PiXiu.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 1){
            int itemId = data.getItemId();
            UserBagItem userBagItem = getUserBagItem(userId, itemId);
            if (userBagItem != null && userBagItem.getNum() > 0) {
                removeItem(userBagItem);
            }
        }else if(data.getRecycleType() == 5){
            //返利补发
            int activityId = Integer.parseInt(data.getParamList().get(0));

            PiXiuUserData userData = new PiXiuUserDataDaoImpl().getPiXiuUserData(activityId, userId);

            if(userData == null || userData.getGetReward() == 1){
                return;
            }
            if(userData.getButtBase() <=0 || userData.getHeadBase() <= 0){
                return;
            }

            if(historyConfig == null || historyConfig.getActivityId() != activityId){
                historyConfig = PiXiuActivityMgr.getConfig();
                if(historyConfig == null || historyConfig.getActivityId() != activityId) {
                    log.info("PiXiuRecycleStrategy activityId{}, config null", activityId);
                    //return;
                }
            }

            Property reward = PiXiuActivityMgr.getProperty(userData, historyConfig);

            userData.setGetReward(1);

            boolean result = new PiXiuUserDataDaoImpl().update(userData);

            if(!result){
                log.info("PiXiuRecycleStrategy activityId{}, update user {} fail", activityId, userId);
                return;
            }

            log.info("PiXiuRecycleStrategy activityId{}, send user {}, {}", activityId, userId, PropertyHelper.parsePropertyToString(reward));

            if(reward.isNothing()){
                return;
            }

            //邮件
            String language = UserMgr.getUserInfo(userId).getLanguage();
            String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_TITLE, language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_CONTENT, language),
                    activityName);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
        }
    }

    @Override
    public boolean isActivityRunning() {
        PiXiuConfig config = PiXiuActivityMgr.getConfig();
        return config != null && ActivityMgr.activityInTime(config.getActivityInfo());
    }
}
